﻿using System;
using System.Windows;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace WholeSlideImageViewer
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private readonly IHost _host;

        public App() => throw new NotSupportedException();

        public App(IHost host)
        {
            _host = host;
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            _host.Start();
            Window mainWindow = _host.Services.GetService<MainWindow>();
            MainWindow = mainWindow;
            mainWindow.Show();
        }

        protected override void OnExit(ExitEventArgs e)
        {
            StopHost();
        }

        private async void StopHost()
        {
            if (_host is IAsyncDisposable asyncDisposable)
            {
                await asyncDisposable.DisposeAsync();
            }
            else
            {
                _host.Dispose();
            }
        }
    }
}
